package com.sky.service.Impl;


import cn.hutool.core.util.NumberUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopingMapper;
import com.sky.service.ShopingService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ShopingServiceImpl implements ShopingService {

    @Autowired
    private ShopingMapper shopingMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //    删除购物车中一个商品
    @Override
    public void deleteById(ShoppingCartDTO shopdto) {

        List<ShoppingCart> all = shopingMapper.getAll(BaseContext.getCurrentId());

        if (shopdto.getDishId() != null) {
            //提取该用户 该菜品的信息
            List<ShoppingCart> Dishcollect = all.stream()
                    .filter(s -> s.getDishId() == shopdto.getDishId())
                    .collect(Collectors.toList());
            log.info("♦♦♦♦♦♦♦♦当前操作删除♦Dishcollect:{}", Dishcollect.toString());
            for (ShoppingCart s : Dishcollect) {
                if (s.getNumber() >= 2) {
                    Integer number = s.getNumber() - 1;
                    shopingMapper.updateNumber(s.getId(), number);
                } else {
                    shopingMapper.deleteById(s.getId());
                }
            }
//删除套餐
        } else {
            //提取该用户 该套餐的信息
            List<ShoppingCart> Setmealcollect = all.stream()
                    .filter(s -> s.getDishId() ==null)
                    .filter(s -> s.getSetmealId() == shopdto.getSetmealId())
                    .collect(Collectors.toList());
            log.info("****====Setmealcollect:{}", Setmealcollect.toString());
            for (ShoppingCart s : Setmealcollect) {
                if (s.getNumber() >= 2) {
                    Integer number = s.getNumber() - 1;
                    shopingMapper.updateNumber(s.getId(), number);
                } else {
                    shopingMapper.deleteById(s.getId());
                }
            }
        }

}

    //    查看购物车
    @Override
    public List<ShoppingCart> getAll() {
        Long userid = BaseContext.getCurrentId();
        List<ShoppingCart> all = shopingMapper.getAll(userid);
        return all;
    }


    //    添加购物车
    @Override
    public void addShopping(ShoppingCartDTO shopdto) {

//        ShoppingCart cart = new ShoppingCart();
//        BeanUtils.copyProperties(shopdto, cart);
//        cart.setUserId(BaseContext.getCurrentId());
//
//        List<ShoppingCart> all = shopingMapper.getAll(BaseContext.getCurrentId());
//
//        if (cart.getDishId() != null) {
//
//            //提取该用户 该菜品的信息
//            List<ShoppingCart> Dishcollect = all.stream().filter(s -> s.getDishId() == cart.getDishId()).collect(Collectors.toList());
//            log.info("****====Dishcollect:{}", Dishcollect.toString());
//            Dish dish = dishMapper.selectById(cart.getDishId());
//            if (Dishcollect.size() != 0) {
//                for (ShoppingCart s : Dishcollect) {
//                    Integer number = s.getNumber() + 1;
//                    shopingMapper.updateNumber(s.getId(), number);
//                }
//            } else {
//                cart.setName(dish.getName());
//                cart.setImage(dish.getImage());
//                cart.setAmount(dish.getPrice());
//                cart.setNumber(1);
//                shopingMapper.addSopping(cart);
//            }
////新增套餐
//        } else {
//            //提取该用户 该套餐的信息
//            List<ShoppingCart> Setmealcollect = all.stream().filter(s -> s.getSetmealId() == cart.getSetmealId()).collect(Collectors.toList());
//            log.info("****====Setmealcollect:{}", Setmealcollect.toString());
//            Setmeal setmeal = setmealMapper.selectById(cart.getSetmealId());
//            if (Setmealcollect.size() != 0) {
//                for (ShoppingCart s : Setmealcollect) {
//                    Integer number = s.getNumber() + 1;
//                    shopingMapper.updateNumber(s.getId(), number);
//                }
//            } else {
//                cart.setName(setmeal.getName());
//                cart.setImage(setmeal.getImage());
//                cart.setAmount(setmeal.getPrice());
//                cart.setNumber(1);
//                shopingMapper.addSopping(cart);
//            }
//        }

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shopdto,shoppingCart);
//        检查是否是该用户的购物车
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);
        log.info("当前id为"+id);
//        检查商品是否在购物车中
        List<ShoppingCart> carts = shopingMapper.list(shoppingCart);

        //如果已经存在，数量加1
        if(!carts .isEmpty()){
            shoppingCart = carts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shopingMapper.updateNumberById(shoppingCart);
        }else {
            //没有商品，判断当前添加到购物车的是菜品还是套餐
            Long dishId = shopdto.getDishId();
            if (dishId == null){
//                dishId为空,添加的是套餐
                Setmeal setmeal = setmealMapper.selectById(shopdto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }else {
//                否则就是菜品
                Dish dish = dishMapper.selectDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
//            最后附上其它值
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shopingMapper.addSopping(shoppingCart);
        }
    }

//    清空购物车
    @Override
    public void deleteShop() {
        Long userid = BaseContext.getCurrentId();
        List<ShoppingCart> all = shopingMapper.getAll(userid);
        for (ShoppingCart c : all) {
            shopingMapper.deleteById(c.getId());
        }

    }

}
